package com.loongtech.bi.action.flow;

import com.loongtech.bi.action.Base2BiSearchAction;
import com.loongtech.bi.manager.flow.HadoopQueryManager;
import com.loongtech.bi.manager.system.ConfigManager;
import com.loongtech.bi.manager.system.GameParamConfigManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.CommonGameParam;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.Time;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 流水查询—声望流水Action
 *
 * @author yuri
 * @date 2017年03月6日
 */
@Controller
@Scope("view")
public class ReputationFlowAction extends Base2BiSearchAction {
	private static final long serialVersionUID = 1L;
    private Date dateStart = new Date(Time.getBeginningOfDay(Time.currentTime()));
    private Date dateEnd = new Date(Time.getBeginningOfDay(Time.currentTime()));
    private int reason = -99999; // 获取原因
    private int comsumeReason = -99999; // 消耗原因

    private String userId;
    private String roleId;
    private String reputationId;

    private Map<String, String> reasonItmes = GameParamConfigManager.getItems(CommonGameParam.REPUTATION_GET_REASON);  // 获取原因Map
    private Map<String, String> comsumereasonItmes = GameParamConfigManager.getItems(CommonGameParam.REPUTATION_COMSUME_REASON);  // 消耗原因Map
    private Map<String, String> reputationTypeRevise = GameParamConfigManager.getItemsRevise(CommonGameParam.REPUTATION_TYPE);  // 声望类型反转
    private Map<String, String> reasonRevise = GameParamConfigManager.getItemsRevise(CommonGameParam.REPUTATION_GET_REASON);  // 获取原因反转Map
    private Map<String, String> comsumeReasonRevise = GameParamConfigManager.getItemsRevise(CommonGameParam.REPUTATION_COMSUME_REASON);  // 消耗原因反转Map

    private List<Object[]> result;  // 结果List


    @Resource
    private ConfigManager configManager;

    @Resource
    HadoopQueryManager hadoopQueryManager;

    @Resource
    private SysLogManager sysLogManager;
    @Resource
    private Session session;

    @PostConstruct
    public void init() {
        result = new ArrayList<>();
    }

    public void search() {
        sysLogManager.addLog(session.getUserName(), "查询声望查询—物品流水");
        result = new ArrayList<>();
        FacesContext context = FacesContext.getCurrentInstance();

        // 开始结束时间不能大于当前时间60天
        if (Time.getInterval(new Date(), dateStart) > 60 || Time.getInterval(new Date(), dateEnd) > 60) {
            context.addMessage(null, new FacesMessage("提示", "开始结束时间不能小于当前时间60天!"));
            return;
        }
        if (Time.getInterval(dateStart, dateEnd) < 0) {
            context.addMessage(null, new FacesMessage("提示", "开始时间不能大于结束时间!"));
            return;
        }
        // 用户ID，角色ID必要有一个非空
        if (StringUtils.isBlank(userId) && StringUtils.isBlank(roleId)) {
            context.addMessage(null, new FacesMessage("提示", "用户ID，角色ID必须至少有一个非空!"));
            return;
        }

        if (HadoopQueryManager.VISIT_QUEUE.offer("ReputationFlowAction")) {
            System.out.println("VISIT_QUEUE offer ReputationFlowAction ok!");

            StringBuilder sql = new StringBuilder();
            sql.append("select logtime,platform,channel,serverid,userid,roleid,rolelevel,viplevel,reputationid,reason,changenum,leftnum from reputation where 1=1 ");
            if (StringUtils.isNotBlank(userId)) {
                sql.append(" and userid = '");
                sql.append(userId);
                sql.append("' ");
            }
            if (StringUtils.isNotBlank(roleId)) {
                sql.append(" and roleid = '");
                sql.append(roleId);
                sql.append("' ");
            }
            if (!"0".equals(queryPlatform)) {
                sql.append(" and platform = ");
                sql.append(queryPlatform);
            }
            if (!"0".equals(queryChannel)) {
                sql.append(" and channel = '");
                sql.append(queryChannel);
                sql.append("' ");
            }
            if (!"0".equals(queryServer)) {
                sql.append(" and serverid = ");
                sql.append(queryServer);
            }
            if (StringUtils.isNotBlank(reputationId)) {
                sql.append(" and reputationid = ");
                sql.append(reputationId);
                sql.append(" ");
            }

            if (reason != -99999) {
                sql.append(" and reason = ");
                sql.append(reason);
                sql.append(" and changenum >= 0 ");
            }

            if (comsumeReason != -99999) {
                sql.append(" and reason = ");
                sql.append(comsumeReason);
                sql.append(" and changenum <= 0 ");
            }

            sql.append(" and dt >= '");
            sql.append(Time.getDateStr(dateStart));
            sql.append("' and dt <= '");
            sql.append(Time.getDateStr(dateEnd));
            sql.append("' ");
            sql.append(" order by logtime desc ");

            System.out.println(sql.toString());
            Entry<List<String>, List<Object[]>> ret = hadoopQueryManager.executeSQL(sql.toString());
            if (null == ret || null == ret.getKey() || ret.getKey().isEmpty() || null == ret.getValue() || ret.getValue().isEmpty()) {
                context.addMessage(null, new FacesMessage("提示", "查询结果为空!"));
                return;
            }
            result = ret.getValue();
        } else {
            System.out.println("VISIT_QUEUE offer ReputationFlowAction failed!");
            context.addMessage(null, new FacesMessage("提示", "当前系统正被访问，为保证服务器稳定，请稍后再试"));
        }
    }

    // 查询声望名称
    public String getReputationType(Object itemType) {
        String key = itemType == null ? "" : Math.abs(Integer.parseInt(itemType.toString())) + "";
        return (reputationTypeRevise.get(key) == null) ? key : reputationTypeRevise.get(key);
    }

    // 查询变化类型名称
    public String getReasonType(Object reasonType, String amount) {
        String key = reasonType == null ? "" : Math.abs(Integer.parseInt(reasonType.toString())) + "";
        if (amount == null) {
            return reasonRevise.get(key) == null ? key : reasonRevise.get(key);
        }
        if (Integer.parseInt(amount) >= 0) {
            return reasonRevise.get(key) == null ? key : reasonRevise.get(key);
        }
        return reasonRevise.get(key) == null ? key : comsumeReasonRevise.get(key);
    }


    @Override
    protected String csvContent() {
        return null;
    }

    @Override
    protected String csvFileName() {
        return null;
    }

    @Override
    public Date getDateStart() {
        return dateStart;
    }

    @Override
    public void setDateStart(Date dateStart) {
        this.dateStart = dateStart;
    }

    public Map<String, String> getReasonItmes() {
        return reasonItmes;
    }

    public void setReasonItmes(Map<String, String> reasonItmes) {
        this.reasonItmes = reasonItmes;
    }

    public Map<String, String> getComsumereasonItmes() {
        return comsumereasonItmes;
    }

    public void setComsumereasonItmes(Map<String, String> comsumereasonItmes) {
        this.comsumereasonItmes = comsumereasonItmes;
    }

    public int getComsumeReason() {
        return comsumeReason;
    }

    public void setComsumeReason(int comsumeReason) {
        this.comsumeReason = comsumeReason;
    }

    public int getReason() {
        return reason;
    }

    public void setReason(int reason) {
        this.reason = reason;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getRoleId() {
        return roleId;
    }

    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }

    public String getReputationId() {
        return reputationId;
    }

    public void setReputationId(String reputationId) {
        this.reputationId = reputationId;
    }

    public List<Object[]> getResult() {
        return result;
    }

    public void setResult(List<Object[]> result) {
        this.result = result;
    }

    @Override
    public Date getDateEnd() {
        return dateEnd;
    }

    @Override
    public void setDateEnd(Date dateEnd) {
        this.dateEnd = dateEnd;
    }
}
